package com.fr.ticket;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.elasticsearch.index.query.QueryBuilder;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * @author 付荣
 * @version 创建时间：2019年4月11日 下午7:27:34
 */
public class LvmamaDataUtil {

	private static final String analyzer_uri = "http://super.lvmama.com/kb_proxy/api/sense/proxy?uri=http%3A%2F%2F172.20.11.148%3A9200%2F_analyze";
	private static final String ticket_search_uri = "http://super.lvmama.com/kb_proxy/api/sense/proxy?uri=http%3A%2F%2F172.20.11.148%3A9200%2Fticket%2Fticket%2F_search";
	private static final String goods_search_uri = "http://super.lvmama.com/kb_proxy/api/sense/proxy?uri=http%3A%2F%2F172.20.11.148%3A9200%2Fgoods%2Fgoods%2F_search";
	private static final String ticket_search_scroll_uri = "http://super.lvmama.com/es_proxy/ticket/ticket/_search?pretty&scroll=10m&size=100";
	private static final String ticket_search_scroll_id_uri = "http://super.lvmama.com/es_proxy/_search/scroll?scroll=10m&pretty&scroll_id=";

	static RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5000) // 从连接池中获取连接的超时时间
			// 与服务器连接超时时间：httpclient会创建一个异步线程用以创建socket连接，此处设置该socket的连接超时时间
			.setConnectTimeout(5000).setSocketTimeout(5000) // socket读数据超时时间：从服务器获取响应数据的超时时间
			// .setProxy(host)
			.build();
	static CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();

	public static List<String> getAnalyzer(String keyword) {
		CloseableHttpResponse response = null;
		AnalyzerParam param = new AnalyzerParam();
		param.setText(keyword);
		List<String> rs = new ArrayList<>();
		try {
			HttpPost httpPost = new HttpPost(analyzer_uri);
			httpPost.setHeader("kbn-version", "4.3.1");
			StringEntity stringEntity = new StringEntity((param.toString()), "utf-8");
			httpPost.setEntity(stringEntity);
			httpPost.setHeader("Content-Type", "application/json");
			response = httpClient.execute(httpPost);
			if (response != null) {
				String httpEntityContent = HttpClientUtil.getHttpEntityContent(response);
				JSONObject object = JSON.parseObject(httpEntityContent);
				List<JSONObject> tokens = (List<JSONObject>) object.get("tokens");
				if (CollectionUtils.isNotEmpty(tokens)) {
					for (JSONObject jo : tokens) {
						rs.add(jo.get("token").toString());
					}
				}
			}
		} catch (Exception e) {

		} finally {
			try {
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return rs;
	}

	// 1-ticket 2-goods
	public static String getTicketProductOrGoods(String queryString, int type) {
		CloseableHttpResponse response = null;
		try {
			HttpPost httpPost = null;
			switch (type) {
			case 1:
				httpPost = new HttpPost(ticket_search_uri);
				break;
			case 2:
				httpPost = new HttpPost(goods_search_uri);
				break;
			default:
				return null;
			}

			httpPost.setHeader("kbn-version", "4.3.1");
			StringEntity stringEntity = new StringEntity(queryString, "utf-8");
			httpPost.setEntity(stringEntity);
			httpPost.setHeader("Content-Type", "application/json");
			response = httpClient.execute(httpPost);
			if (response != null) {
				String httpEntityContent = HttpClientUtil.getHttpEntityContent(response);
				return httpEntityContent;
			}
		} catch (Exception e) {

		} finally {
			try {
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	// ticket-scroll
	public static String getTicketProductScroll(String queryString, String scrollId) {
		CloseableHttpResponse response = null;
		try {
			HttpGet httpGet = null;
			if (StringUtils.isNotBlank(scrollId)) {
				httpGet = new HttpGet(ticket_search_scroll_id_uri + scrollId);
			} else {
				httpGet = new HttpGet(ticket_search_scroll_uri);
			}
			httpGet.setHeader("kbn-version", "4.3.1");
//			StringEntity stringEntity = new StringEntity(queryString, "utf-8");
//			httpGet.setEntity(stringEntity);
			httpGet.setHeader("Content-Type", "application/json");
			response = httpClient.execute(httpGet);
			if (response != null) {
				String httpEntityContent = HttpClientUtil.getHttpEntityContent(response);
				return httpEntityContent;
			}
		} catch (Exception e) {

		} finally {
			try {
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public static void main(String[] args) {
		// System.out.println(getAnalyzer("上海马戏团"));
		// System.out.println(getTicketProductOrGoods("{}", 1));
		// System.out.println(getTicketProductOrGoods("{\"query\":{\"term\":{\"productId\":{\"value\":\"851916\"}}}}",
		// 1));

	}
}
